package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.domain.Category;
import com.ruoyi.domain.dto.CategoryDto;
import com.ruoyi.mapper.CategoryMapper;
import com.ruoyi.service.ICategoryService;
import com.ruoyi.utils.DtoToEntityConverter;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public AjaxResult addCategory(CategoryDto categoryDto) {

        try{
            Category category = DtoToEntityConverter.convertDtoToEntity(categoryDto, Category.class);
            int count = categoryMapper.addCategory(category);
            return (count!=0)? AjaxResult.success("增加图书分类成功"):AjaxResult.error(201,"增加图书分类失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public AjaxResult deleteCategory(Long id) {
        try{
            int count = categoryMapper.deleteCategory(id);
            return (count!=0)? AjaxResult.success("删除图书分类成功"):AjaxResult.error(201,"删除图书分类失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public AjaxResult selectCategory() {
        try{
            List<Category> categoryList = categoryMapper.selectCategory();
            return (categoryList!=null)? AjaxResult.success(categoryList):AjaxResult.error(201,"查询图书分类失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
